package com.googlecode.alfrescocachingserver.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.ws.rs.HttpMethod;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.googlecode.alfrescocachingserver.model.CacheEntry;
import com.googlecode.alfrescocachingserver.model.WebScriptCacheKey;
import com.googlecode.alfrescocachingserver.model.WebScriptUri;
import com.googlecode.alfrescocachingserver.services.CacheService;

@Controller
public class CacheRestController
{
    @Autowired
    private CacheService cacheService;

    @RequestMapping(method = {RequestMethod.GET}, value = "/removeEntry")
    public ModelAndView removeCacheEntry(@RequestParam(value = "method", defaultValue = HttpMethod.GET, required = false) String httpMethod, @RequestParam(value = "key") String cacheKey)
    {
        WebScriptUri webScriptUri = new WebScriptUri(httpMethod, cacheKey);
        
        WebScriptCacheKey webScriptCacheKey = new WebScriptCacheKey();
        webScriptCacheKey.setWebScriptUri(webScriptUri);
        
        boolean removed = cacheService.removeCacheEntry(webScriptCacheKey);
        
        List<String> removedKeys = new ArrayList<String>();
        
        if (removed)
        {
            removedKeys.add(cacheKey);
        }
        
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("cacheRemove");
        modelAndView.addObject("removedCacheKeys", removedKeys);

        return modelAndView;
    }

    @RequestMapping(method = {RequestMethod.GET}, value = "/removeEntries")
    public ModelAndView removeCacheEntries(@RequestParam String id)
    {
        Set<String> removedKeys = cacheService.removeCacheEntries(id);
        
//        List<String> removedKeys = new ArrayList<String>();
//        
//        removedKeys.add("key1");
//        removedKeys.add("key2");
//        removedKeys.add("key3");
        
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("cacheRemove");
        modelAndView.addObject("removedCacheKeys", removedKeys);

        return modelAndView;
    }

    @RequestMapping(method = {RequestMethod.GET}, value = "/flush")
    public ModelAndView flushCache(@RequestParam("name") String cacheName)
    {
        Set<String> cacheNames = cacheService.flushCache(cacheName);
        
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("cacheFlush");
        modelAndView.addObject("cacheNames", cacheNames);

        return modelAndView;
    }

    @RequestMapping(method = {RequestMethod.GET}, value = "/refreshEntry")
    public ModelAndView refreshCacheEntry(@RequestParam(value = "method", defaultValue = HttpMethod.GET, required = false) String httpMethod, @RequestParam(value = "key") String cacheKey)
    {
        WebScriptUri webScriptUri = new WebScriptUri(httpMethod, cacheKey);
        
        WebScriptCacheKey webScriptCacheKey = new WebScriptCacheKey();
        webScriptCacheKey.setWebScriptUri(webScriptUri);

        // remove the cache entry in case it already exists
        cacheService.removeCacheEntry(webScriptCacheKey);

        // retrieve the new data and populate the cache entry
        CacheEntry cacheEntry = cacheService.getCacheEntry(webScriptCacheKey);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("cacheRefresh");

        if (cacheEntry != null)
        {
            modelAndView.addObject("cacheKey", cacheKey);
        }
        
        return modelAndView;
    }
}
